home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / prog / mod2tutb.zip / LOOPDEMO.MOD < prev    next >
Text File  |  1989-01-18  |  3KB  |  116 lines

  1.                                          (* Chapter 4 - Program 1 *)
  2. MODULE LoopDemo;
  3.  
  4. FROM InOut IMPORT WriteString, WriteInt, WriteLn, Write;
  5.  
  6. CONST Where = 11;
  7.  
  8. VAR  Index   : INTEGER;
  9.      What    : INTEGER;
  10.      Letter  : CHAR;
  11.  
  12. BEGIN
  13.  
  14.    WriteString("REPEAT loop     = ");
  15.    Index := 0;
  16.    REPEAT
  17.      Index := Index + 1;
  18.      WriteInt(Index,5);
  19.    UNTIL Index = 5;          (* This can be any BOOLEAN expression *)
  20.    WriteLn;
  21.  
  22.    WriteString("WHILE loop      = ");
  23.    Index := 0;
  24.    WHILE Index < 5 DO        (* This can be any BOOLEAN expression *)
  25.       Index := Index + 1;
  26.       WriteInt(Index,5);
  27.    END;
  28.    WriteLn;
  29.  
  30.    WriteString("First FOR loop  = ");
  31.    FOR Index := 1 TO 5 DO
  32.       WriteInt(Index,5);
  33.    END;
  34.    WriteLn;
  35.  
  36.    WriteString("Second FOR loop = ");
  37.    FOR Index := 5 TO 25 BY 4 DO
  38.       WriteInt(Index,5);
  39.    END;
  40.    WriteLn;
  41.  
  42. (* Note - The four loops above could use a CARDINAL type variable
  43.           in place of the INTEGER type variable Index.  The next 2
  44.           examples must use an INTEGER type variable because it
  45.           must be capable of storing a negative value.            *)
  46.  
  47.    WriteString("Third FOR loop  = ");
  48.    FOR Index := 5 TO -35 BY -7 DO
  49.       WriteInt(Index,5);
  50.    END;
  51.    WriteLn;
  52.  
  53.    What := 16;
  54.    FOR Index := (What - 21) TO (What * 2) BY Where DO
  55.       WriteString("Fourth FOR loop = ");
  56.       WriteInt(Index,5);
  57.       WriteLn;
  58.    END;
  59.  
  60. (* Note - The next two loops are demonstrations of using a CHAR
  61.           type variable to index a FOR loop.                      *)
  62.  
  63.    FOR Letter := "A" TO 'Z' DO
  64.       Write(Letter);
  65.    END;
  66.    WriteLn;
  67.  
  68.    FOR Letter := 'z' TO 'a' BY -1 DO
  69.       Write(Letter);
  70.    END;
  71.    WriteLn;
  72.  
  73. (* Note - The following loop contains an EXIT which is a way to get
  74.           out of the loop in the middle.                           *)
  75.  
  76.    Index := 1;
  77.    LOOP
  78.       WriteString("In the EXIT loop ");
  79.       WriteInt(Index,5);
  80.       IF Index = 5 THEN
  81.          WriteLn;
  82.          EXIT;
  83.       END;
  84.       WriteString("  We are still in the loop.");
  85.       WriteLn;
  86.       Index := Index + 1;
  87.    END;
  88.  
  89. END LoopDemo.
  90.  
  91.  
  92.  
  93.  
  94. (* Result of execution
  95.  
  96. REPEAT loop     =     1    2    3    4    5
  97. WHILE loop      =     1    2    3    4    5
  98. First FOR loop  =     1    2    3    4    5
  99. Second FOR loop =     5    9   13   17   21   25
  100. Third FOR loop  =     5   -2   -9  -16  -23  -30
  101. Fourth FOR loop =    -5
  102. Fourth FOR loop =     6
  103. Fourth FOR loop =    17
  104. Fourth FOR loop =    28
  105. ABCDEFGHIJKLMNOPQRSTUVWXYZ
  106. zyxwvutsrqponmlkjihgfedcba
  107. In the EXIT loop     1  We are still in the loop.
  108. In the EXIT loop     2  We are still in the loop.
  109. In the EXIT loop     3  We are still in the loop.
  110. In the EXIT loop     4  We are still in the loop.
  111. In the EXIT loop     5
  112.  
  113. *)
  114.  
  115.  
  116.